package com.efast.dmom.mes.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.dmom.mes.bean.OperationWorkorder;
import com.efast.dmom.mes.bean.ProductionImplementation;
import com.efast.dmom.mes.bean.ProductionImplementationAccept;
import com.efast.dmom.mes.bean.ProductionImplementtationUser;
import com.efast.dmom.mes.bean.RouteStep;
import com.efast.dmom.mes.bean.WmsStorage;
import com.efast.dmom.mes.dao.bean.ProductionImplementationDao;
import com.efast.dmom.mes.dao.entity.OperationWorkorderDao;
import com.efast.dmom.mes.dao.entity.PlanDao;
import com.efast.dmom.mes.datahandlers.entity.RouteDataHandler;
import com.efast.dmom.mes.entity.OperationWorkorderEntity;
import com.efast.dmom.mes.entity.PartEntity;
import com.efast.dmom.mes.entity.PlanEntity;
import com.efast.dmom.mes.entity.ProductRouteEntity;
import com.efast.dmom.mes.entity.ProductionPiecePartEntity;
import com.efast.dmom.mes.entity.StartWorkEntity;
import com.efast.dmom.mes.service.ProductionImplementationService;
import com.efast.dmom.mes.service.ProductionPiecePartService;
import com.efast.dmom.mes.service.StartWorkService;
import com.efast.dmom.mes.service.WmsStorageService;
import com.efast.dmom.mes.util.WmsOrderStatusUtil;

@Service
public class ProductionImplementationServiceImpl implements ProductionImplementationService {
	
	

	@Autowired
	private ProductionImplementationDao productionImplementationDao;

	@Autowired
	private StartWorkService startWorkService;

	@Autowired
	private ProductionPiecePartService productionPiecePartService;

	@Autowired
	private RouteDataHandler routeDataHandler;
	
	@Autowired
	private WmsStorageService wmsStorageService;
	
	@Autowired
	private PlanDao planDao;
	
	@Autowired
	private OperationWorkorderDao operationWorkorderDao;
	
	@Override
	public List<ProductRouteEntity> getProductionPart() {
		//通过产品与工艺路线关系获取产品信息
		List<ProductRouteEntity> productRouteEntity = productionImplementationDao.getProductionPart();
		return productRouteEntity;
	}

	@Override
	public ProductionImplementation confirm(ProductionImplementationAccept acceptBean) {
		if (acceptBean != null) {
			ProductionImplementation productionImplementation = new ProductionImplementation();
			List<StartWorkEntity> startWorkEntityList = startWorkService.getStartWorkByPartIdAndOperationId(acceptBean);
			List<ProductionPiecePartEntity> productionPiecePartEntityList = productionPiecePartService
					.getProductionPiecePartList(acceptBean);
			productionImplementation.setStartWorkEntityList(startWorkEntityList);
			productionImplementation.setProductionPiecePartEntityList(productionPiecePartEntityList);
			
			return productionImplementation;
		}
		return null;
	}

	@Override
	public List<RouteStep> getAllRouteStepByRouteKey(long routeKey) {
		List<RouteStep> routeStepList = routeDataHandler.getAllRouteStepByRouteKey(routeKey);
		if (routeStepList != null && routeStepList.size() > 0) {
			routeStepList.get(0).getInnerData().setOperationType(WmsOrderStatusUtil.STARTOPERATIONTYPE);
			routeStepList.get(routeStepList.size() - 1).getInnerData().setOperationType(WmsOrderStatusUtil.ENDOPERATIONTYPE);
		    for (int i= 1;i < routeStepList.size()-1;i++) {
		    	routeStepList.get(i).getInnerData().setOperationType(WmsOrderStatusUtil.NORMALOPERATIONTYPE);
		    }
		}
		return routeStepList;
	}

	@Override
	public List<PartEntity> getPartEntityList(long warehouseBasId) {
		return wmsStorageService.getPartEntityList(warehouseBasId);
	}

	@Override
	public List<ProductionImplementtationUser> getUserName() {
		 List<ProductionImplementtationUser> list = productionImplementationDao.getUserName();
		 return list;
	}

	@Override
	public List<WmsStorage> getwareHouser() {
		return productionImplementationDao.getwareHouser();
	}

	@Override
	public List<PlanEntity> getPlanOrderByPartId(OperationWorkorder operationWorkorder) {
		List<PlanEntity> byPartId = planDao.getByPartId(operationWorkorder.getPartId());
		List<PlanEntity> arrayList = new ArrayList<PlanEntity>();
		for (PlanEntity planEntity : byPartId) {
			 OperationWorkorderEntity operationWorkorderEntity = new OperationWorkorderEntity();
			 operationWorkorderEntity.setPlanNo(planEntity.getPlanNo());
			int selectCount = operationWorkorderDao.selectCount(operationWorkorderEntity);
			if (selectCount > 0) {
				arrayList.add(planEntity);
			}
		}
		return arrayList;
	}

	@Override
	public List<OperationWorkorderEntity> getOperationPlanByOperationAndPlan(
			OperationWorkorderEntity operationWorkorderEntity) {
		List<OperationWorkorderEntity> select = operationWorkorderDao.select(operationWorkorderEntity);
		return select;
	}

}
